﻿#include "sydata.h"
#include <fstream>
#include "log.h"
using sy::tools::Log;

// 顶点数据文件路径
// (这样可以确保在跨文件使用静态变量 filePath 时，它已经被初始化，防止出现静态变量初始化顺序带来的)问题
std::string& FilePath()
{
	static std::string filePath = "./data.txt";
	return filePath;
}

std::vector<float>& sy::data::Vertex()
{
	static std::vector<float> instance;
	return instance;
}

std::vector<int>& sy::data::Index()
{
	static std::vector<int> instance;
	return instance;
}

void sy::data::LoadData(std::vector<float>& buf, std::string& filePath)
{
	using sy::data::Vertex;
	std::fstream file(filePath);
	if (!file.is_open())
	{
		Log().Write("数据文件打开失败！！！");
	}
	float x, y, z;
	while (file >> x >> y >> z)
	{
		buf.push_back(x);
		buf.push_back(y);
		buf.push_back(z);
		buf.push_back(1.0f);
	}
	file.close();
}

void sy::data::GenData(int count)
{
	count = count / 6;
	std::fstream file(FilePath(), std::ios::ate | std::ios::out);

	if (!file.is_open())
	{
		Log().Write("生成数据时，打开数据文件失败！！！");
		return;
	}

	float x, y, z;
	srand(time(nullptr));
	
	// 前面
	for (int i = 0; i < count; ++i)
	{
		x = ((float)(rand() % 20000) / 10000.0) - 1.0f;
		y = ((float)(rand() % 20000) / 10000.0) - 1.0f;
		z = 1.0f;
		file << x << " " << y << " " << z << std::endl;
	}

	// 后面
	for (int i = 0; i < count; ++i)
	{
		x = ((float)(rand() % 20000) / 10000.0) - 1.0f;
		y = ((float)(rand() % 20000) / 10000.0) - 1.0f;
		z = -1.0f;
		file << x << " " << y << " " << z << std::endl;
	}

	// 左面
	for (int i = 0; i < count; ++i)
	{
		z = ((float)(rand() % 20000) / 10000.0) - 1.0f;
		y = ((float)(rand() % 20000) / 10000.0) - 1.0f;
		x = -1.0f;
		file << x << " " << y << " " << z << std::endl;
	}

	// 右面
	for (int i = 0; i < count; ++i)
	{
		z = ((float)(rand() % 20000) / 10000.0) - 1.0f;
		y = ((float)(rand() % 20000) / 10000.0) - 1.0f;
		x = 1.0f;
		file << x << " " << y << " " << z << std::endl;
	}

	// 上面
	for (int i = 0; i < count; ++i)
	{
		x = ((float)(rand() % 20000) / 10000.0) - 1.0f;
		z = ((float)(rand() % 20000) / 10000.0) - 1.0f;
		y = 1.0f;
		file << x << " " << y << " " << z << std::endl;
	}

	// 下面
	for (int i = 0; i < count; ++i)
	{
		x = ((float)(rand() % 20000) / 10000.0) - 1.0f;
		z = ((float)(rand() % 20000) / 10000.0) - 1.0f;
		y = -1.0f;
		file << x << " " << y << " " << z << std::endl;
	}
	file.close();
}









float data[10][4] = {
	{    0,    1, 0.25, 1},		// A
	{ 0.95, 0.30, 0.25, 1},		// B
	{ 0.58,-0.81, 0.25, 1},		// C
	{-0.58,-0.81, 0.25, 1},		// D
	{-0.95, 0.30, 0.25, 1},		// E
	{    0,    1,-0.25, 1},		// A'
	{ 0.95, 0.30,-0.25, 1},		// B'
	{ 0.58,-0.81,-0.25, 1},		// C'
	{-0.58,-0.81,-0.25, 1},		// D'
	{-0.95, 0.30,-0.25, 1}		// E'
};

int index[15][2] = {
	{ 0, 2},
	{ 0, 3},
	{ 1, 4},
	{ 1, 3},
	{ 2, 4},
	{ 0, 5},
	{ 1, 6},
	{ 2, 7},
	{ 3, 8},
	{ 4, 9},
	{ 5, 8},
	{ 5, 7},
	{ 6, 9},
	{ 6, 8},
	{ 9, 7}
};